package com.example.inventoryservice.service;

import com.example.inventoryservice.bean.Inventory;
import com.example.inventoryservice.dao.InventoryDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class InventoryService {
    @Resource
    private InventoryDao dao;

    public Inventory findByProductId(Long productId) {
        return dao.findByProductId(productId);
    }

    @Transactional
    public boolean lockStock(Long productId, Integer quantity) {
        if (quantity <= 0) return false;
        return dao.lockStock(productId, quantity) > 0;
    }

    @Transactional
    public boolean releaseStock(Long productId, Integer quantity) {
        if (quantity <= 0) return false;
        return dao.releaseStock(productId, quantity) > 0;
    }

    @Transactional
    public boolean deductStock(Long productId, Integer quantity) {
        if (quantity <= 0) return false;
        return dao.deductStock(productId, quantity) > 0;
    }
}